import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;

import javax.microedition.io.ConnectionNotFoundException;
import javax.microedition.io.Connector;
import javax.microedition.io.SocketConnection;

import javax.microedition.lcdui.Image;

public class SplashImageThread implements Runnable
{
	private InputStream is;
	private Image screen;
	private SocketConnection sc;
	private SplashImage splashi;
	private String url;
	private boolean error;
	
	public SplashImageThread(String url, SplashImage si)
	{
		this.url = url;
		this.splashi = si;
		this.error = false;
	}
	
	public void run()
	{
		try
		{
			this.sc = (SocketConnection) Connector.open(url);
			this.is = sc.openInputStream();	
			
			/* Receive */
			splashi.setIcon(Image.createImage("/receive.png"));
			splashi.setTitle("prejemam");

			this.screen = Image.createImage(is);
			
			this.splashi.done();
			this.splashi.setTitle("Prenos koncan.");

			is.close();
			sc.close();
		}
		catch(ConnectionNotFoundException cnfe)
		{
			this.error = true;
			this.splashi.setTitle("Ne morem vzpostaviti povezave.");
		}
		catch(Exception e)
		{
			e.printStackTrace();
			/* Connection interrupted */
			this.error = true;
		}
		
		if(error)
		{
			this.splashi.setTitle("Napaka.");
		}
		
	}
	
	public boolean error()
	{
		return this.error;
	}

	public void connect()
	{
		new Thread(this).start();
	}
	
	public Image getScreen()
	{
		return this.screen;
	}

	
}
